package cn.danger.com.view.adapter;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.RecyclerSwipeAdapter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.danger.com.activity.R;
import cn.danger.com.model.entity.Memo;
import cn.danger.com.presenter.MemoPresenter;
import cn.danger.com.util.AnimatorUtil;
import cn.danger.com.util.DateUtil;
import cn.danger.com.util.DensityUtil;

/**
 * Created by 俊 on 2017/1/6.
 */
public class MemoRecyclerViewAdapter extends RecyclerSwipeAdapter<MemoRecyclerViewAdapter.SimpleViewHolder> implements View.OnClickListener {

    private Context context;
    private List<Memo> list;
    private TextView bar_title_textView;
    private AppCompatButton edit_button;
    private FloatingActionButton delete_button;
    private MemoPresenter presenter;
    private Set<Integer> check_positions = new HashSet<>();

    public MemoRecyclerViewAdapter(Context context, TextView bar_title_textView, AppCompatButton edit_button, FloatingActionButton delete_button, MemoPresenter presenter) {
        this.context = context;
        list = presenter.getMemos();
        this.bar_title_textView = bar_title_textView;
        this.edit_button = edit_button;
        this.delete_button = delete_button;
        this.presenter = presenter;
    }


    @Override
    public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.listview_item_layout, parent, false);
        SimpleViewHolder holder = new SimpleViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(final SimpleViewHolder simpleViewHolder, final int position) {
        simpleViewHolder.swipe.setShowMode(SwipeLayout.ShowMode.PullOut);
        simpleViewHolder.delete_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.deleteMemo(list.get(position));
                simpleViewHolder.swipe.close();
                mItemManger.closeAllItems();
            }
        });
        simpleViewHolder.check_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    check_positions.add(position);
                } else {
                    if (check_positions.contains(position)) {
                        check_positions.remove(position);
                    }
                }
                if (check_positions.isEmpty()) {
                    bar_title_textView.setText("备忘录");
                    AnimatorUtil.setGoneAnimator(delete_button);
                } else {
                    bar_title_textView.setText("已选定" + check_positions.size() + "项");
                    AnimatorUtil.setVisibleAnimator(delete_button);
                }
                Log.i("danger", check_positions.toString());
            }
        });
        Memo memo = list.get(position);
        simpleViewHolder.title_textView.setText(memo.getTitle());
        simpleViewHolder.content_textView.setText(memo.getContent());
        simpleViewHolder.date_textView.setText(DateUtil.getDate(memo.getDate()));
        if (edit_button.getText().equals("编辑")) {
            simpleViewHolder.check_box.setVisibility(View.GONE);
//            AnimatorUtil.setGoneAnimator(simpleViewHolder.check_box, simpleViewHolder.body_layout, context);
            simpleViewHolder.check_box.setChecked(false);
            simpleViewHolder.delete_layout.setLayoutParams(new FrameLayout.LayoutParams(DensityUtil.dip2px(context, 80), DensityUtil.dip2px(context, 56)));
        } else if (edit_button.getText().equals("取消")) {
            simpleViewHolder.check_box.setVisibility(View.VISIBLE);
            mItemManger.closeAllItems();
//            AnimatorUtil.setVisibleAnimator(simpleViewHolder.check_box, simpleViewHolder.body_layout, context);
            AnimatorUtil.setGoneAnimator(delete_button);
            simpleViewHolder.delete_layout.setLayoutParams(new FrameLayout.LayoutParams(0, 0));
        }
        simpleViewHolder.itemView.setTag(position);
        mItemManger.bindView(simpleViewHolder.itemView, position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getSwipeLayoutResourceId(int i) {
        return R.id.swipe;
    }

    public class SimpleViewHolder extends RecyclerView.ViewHolder {

        public SwipeLayout swipe;
        private LinearLayout body_layout;
        public RelativeLayout delete_layout;
        public AppCompatCheckBox check_box;
        public TextView title_textView;
        public TextView date_textView;
        public TextView content_textView;

        public SimpleViewHolder(View convertView) {
            super(convertView);
            swipe = (SwipeLayout) convertView.findViewById(R.id.swipe);
            body_layout = (LinearLayout) convertView.findViewById(R.id.body_layout);
            delete_layout = (RelativeLayout) convertView.findViewById(R.id.delete_layout);
            check_box = (AppCompatCheckBox) convertView.findViewById(R.id.check_box);
            title_textView = (TextView) convertView.findViewById(R.id.title_textView);
            date_textView = (TextView) convertView.findViewById(R.id.date_textView);
            content_textView = (TextView) convertView.findViewById(R.id.content_textView);
        }

    }

    public void setList() {
        list = presenter.getMemos();
    }

    public Set<Integer> getCheckPositions() {
        return check_positions;
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClick(v, (int) v.getTag());
        }
    }
}
